class_name UIDamageNumber
extends Control

@export var start_pos_y: float = -7
@export var middle_pos_y: float = -23
@export var end_pos_y: float = -39
@export var normal_show_duration: float = 1.5
@export var tween_duration: float = 0.5
@export var normal_alpha: float = 0.8

@onready var label: Label = $Label
var damage_number: int = 10


func set_damage_number(damage_number: int, scale: float = 1.0, color: Color = Color("884b2b")):
	self.damage_number = damage_number
	label.scale = Vector2(scale, scale)
	label.label_settings.font_color = color


func start_anim():
	label.position.y = start_pos_y
	self.modulate.a = 0
	
	var tween = create_tween()
	tween.tween_property(label, "position:y", middle_pos_y, tween_duration) \
	.set_trans(Tween.TRANS_SINE) \
	.set_ease(Tween.EASE_OUT)
	
	var tween_alpha = create_tween()
	tween_alpha.tween_property(self, "modulate:a", normal_alpha, tween_duration) \
	.set_trans(Tween.TRANS_SINE) \
	.set_ease(Tween.EASE_OUT)
	
	var tween_number = create_tween()
	tween_number.tween_method(on_number_change, 0.0, damage_number, tween_duration) \
	.set_trans(Tween.TRANS_SINE) \
	.set_ease(Tween.EASE_OUT)  # 0 到 10，2 秒
	
	await tween.finished
	modulate.a = normal_alpha
	label.position.y = middle_pos_y
	await get_tree().create_timer(normal_show_duration).timeout
	
	tween = create_tween()
	tween.tween_property(label, "position:y", end_pos_y, tween_duration) \
	.set_trans(Tween.TRANS_SINE) \
	.set_ease(Tween.EASE_IN)
	
	tween_alpha = create_tween()
	tween_alpha.tween_property(self, "modulate:a", 0.0, tween_duration) \
	.set_trans(Tween.TRANS_SINE) \
	.set_ease(Tween.EASE_OUT)
	
	await tween.finished
	queue_free()
	

func on_number_change(number):
	label.text = "-" + str(int(number))
	
	
